package com.wq.httpclientcore.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wq.httpclientcore.convert.Convertor;
import com.wq.httpclientcore.entities.po.DbQueryConfig;
import com.wq.httpclientcore.entities.req.query.DbQueryConfigREQ;
import com.wq.httpclientcore.entities.req.save.DbQueryConfigSaveREQ;
import com.wq.httpclientcore.entities.vo.DbQueryConfigVO;
import com.wq.httpclientcore.model.Result;
import com.wq.httpclientcore.service.DbQueryConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.*;

/**
 * @author wangqi
 * @since 2025-03-18 09:05:42
 */
@Tag(name = "数据库查询配置")
@RequiredArgsConstructor
@RestController
@RequestMapping("/v1/module/dbQueryConfig")
public class DbQueryConfigController {

    private final DbQueryConfigService dbQueryConfigService;

    @Operation(summary = "查询列表")
    @Parameters({@Parameter(name = "pageNo", hidden = true), @Parameter(name = "pageSize", hidden = true)})
    @GetMapping("/list")
    public Result<List<DbQueryConfigVO>> list(@ParameterObject DbQueryConfigREQ req) {
        List<DbQueryConfig> poList = dbQueryConfigService.list(req);
        return Result.success(Convertor.INSTANCE.convertDbQueryConfigs(poList));
    }

    @Operation(summary = "分页查询")
    @GetMapping("/page")
    public Result<IPage<DbQueryConfigVO>> page(@ParameterObject DbQueryConfigREQ req) {
        Page<DbQueryConfig> page = dbQueryConfigService.pageList(req);
        IPage<DbQueryConfigVO> result = page.convert(Convertor.INSTANCE::convert);
        return Result.success(result);
    }

    @Operation(summary = "查询详情")
    @GetMapping("/{id}")
    public Result<DbQueryConfigVO> getById(@PathVariable("id") Long id) {
        DbQueryConfig data = dbQueryConfigService.getById(id);
        if (null == data) {
            return Result.fail("数据不存在或已删除");
        }

        return Result.success(Convertor.INSTANCE.convert(data));
    }

    @Operation(summary = "新增")
    @PostMapping
    public Result<?> add(@RequestBody DbQueryConfigSaveREQ req) {
        DbQueryConfig po = Convertor.INSTANCE.convert(req);
        return Result.success(dbQueryConfigService.save(po));
    }

    @Operation(summary = "修改")
    @PutMapping("/{id}")
    public Result<?> edit(@PathVariable("id") Long id, @RequestBody DbQueryConfigSaveREQ req) {
        DbQueryConfig po = Convertor.INSTANCE.convert(req);
        po.setId(id);
        po.setUpdatedAt(LocalDateTime.now());
        return Result.success(dbQueryConfigService.updateById(po));
    }

    @Operation(summary = "删除")
    @DeleteMapping("/{ids}")
    public Result<?> delete(@PathVariable Long[] ids) {
        dbQueryConfigService.removeByIds(Arrays.asList(ids));
        return Result.success("删除成功");
    }

}
